package com.zretc.device.dao;
/**
 * 仓库管理员dao
 */
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zretc.dbtools.DBTools;
import com.zretc.device.entity.Admin;
import com.zretc.device.entity.Device_Admin;


public class Device_AdminDao {
	/**
	 * 添加管理员
	 * @param user user对象
	 * @return  true 添加成功
	 */
	public boolean addDevAdmin(Device_Admin devAdmin) {
		//调用工具
		String sql="insert into device_admin values (?,?,?)";
		int n=DBTools.getUpdate(sql,devAdmin.getUsername(),devAdmin.getUsercode(),devAdmin.getPassword());
		return n>0;
	}
	/**
	 * 判断用户输入是否正确
	 * @param username
	 * @param password
	 * @return
	 */
	public boolean dologin(String username,String password) {
		int n=0;
		//调用工具
		String sql="select count(username) from device_admin where username=? and password=?";
		ResultSet rs=DBTools.getQuery(sql, username,password);
		try {
			//遍历
			if (rs.next()) {
				n=rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return n==1;
	}
	
	/**
	 * 删除用户
	 * 
	 * @param userName 参数为用户名
	 * @return
	 */
	public boolean delDevAdmin(String userName) {
		// 调用工具
		String sql = "delete from device_admin where username=?";
		int n = DBTools.getUpdate(sql, userName);
		return n > 0;
	}
	
	/**
	 * 修改密码的方法
	 * 
	 * @return
	 */
	public boolean modDevAdmin(Device_Admin devAdmin) {
		String sql = "update device_admin set password=? where username=?";
		int n = DBTools.getUpdate(sql, devAdmin.getPassword(),devAdmin.getUsername());
		return n > 0;
	}
	

	/**
	 * 查询所有
	 * 
	 * @return
	 */
	public List<Device_Admin> findDevAdminAll() {
		// 创建集合
		List<Device_Admin> list = new ArrayList<Device_Admin>();
		String sql = "select * from device_admin";
		ResultSet rs = DBTools.getQuery(sql, null);
		try {
			while (rs.next()) {
				// 用户名
				String username = rs.getString(1);
				// 昵称
				String usercode = rs.getString(2);
				// 密码
				String password = rs.getString(3);
				Device_Admin dadmin = new Device_Admin(username, usercode, password);
				// 添加到集合中
				list.add(dadmin);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return list;
	}
	
	/**
	 * 根据用户名查询
	 * 
	 * @return
	 */
	public Device_Admin findDevAdminById(String userName) {
		// 创建对象
		Device_Admin dadmin =null;
		String sql = "select * from device_admin where username=?";
		ResultSet rs = DBTools.getQuery(sql, userName);
		try {
			if (rs.next()) {
				// 用户名
				String username = rs.getString(1);
				// 昵称
				String usercode = rs.getString(2);
				// 密码
				String password = rs.getString(3);
				dadmin = new Device_Admin(userName, usercode, password);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return dadmin;
	}
	
	
	
	
	
	
	
	
	
}
